﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;
using Code.Commen;
using System.Security.Cryptography;

namespace Code.BLL
{
    public class ManagerManage:ManageBase<Admin>
    {
        public int Add(Admin admin)
        {
            if (string.IsNullOrEmpty(admin.LoginName))
                throw new ValidateException("登录名不能为空", "LoginName");
            if (admin.LoginName.Length < 4 || admin.LoginName.Length > 20)
                throw new ValidateException("请输入4-20个字符", "LoginName");
            if (string.IsNullOrEmpty(admin.Password))
                throw new ValidateException("登录密码不能为空", "Password");
            if (admin.Password.Length < 6 || admin.Password.Length > 12)
                throw new ValidateException("请输入6-12个字符", "Password");
            if (!CheckLoginName(admin.LoginName))
                throw new ValidateException("用户名已存在","LoginName");

            int rs = 0;
            admin.ID =Guid.NewGuid();
            admin.Salt = Guid.NewGuid().ToString();
            var md5 = MD5.Create();
            admin.Password = md5.ComputeHash(admin.Password, admin.Salt);
            rs = base.Insert(new { admin.ID, admin.LoginName, admin.Password, admin.Salt });
            return rs;
        }

        /// <summary>
        /// 检查登录名是否可用
        /// </summary>
        public bool CheckLoginName(string loginName)
        {
            return !base.GetTable<Admin>().Select(p => p.LoginName).Contains(loginName);
        }

        public Admin CheckPwd(string loginName, string password)
        {
            var admin = base.GetTable<Admin>().FirstOrDefault(p => p.LoginName == loginName);
            if (admin == null)
                throw new ValidateException("用户名或密码不正确", "error");
            if (admin.State == AdminState.Disabled.ValueShort())
                throw new ValidateException("账号目前不可用", "LoginError");
            var md5 = MD5.Create();
            password = md5.ComputeHash(password, admin.Salt);
            if (password != admin.Password)
                throw new ValidateException("用户名或密码不正确", "error");
            
            return admin;
        }

        public int Del(params Guid[] ids)
        {
            return base.Delete<Admin>(new { ID = new ComparValue(Comparison.In, ids) });
        }
        public int State(Guid id,AdminState state)
        {
            return base.Update<Admin>(new { State = state.ValueShort() }, new { ID = id });
        }

        public int Edit(Admin admin)
        {
            var up = new NameValue<string, object>();
            if (string.IsNullOrEmpty(admin.LoginName))
                throw new ValidateException("登录名不能为空", "LoginName");
            else if (base.GetTable<Admin>().Any(p => p.ID != admin.ID && p.LoginName == admin.LoginName))
                throw new ValidateException("用户名已存在", "LoginName");
            else if (admin.LoginName.Length < 4 || admin.LoginName.Length > 20)
                throw new ValidateException("请输入4-20个字符", "LoginName");
            else
                up.Add("LoginName", admin.LoginName);
            if (!string.IsNullOrEmpty(admin.Password))
            {
                if (admin.Password.Length < 6 || admin.Password.Length > 12)
                    throw new ValidateException("请输入6-12个字符", "Password");
                admin.Salt = Guid.NewGuid().ToString();
                var md5 = MD5.Create();
                admin.Password = md5.ComputeHash(admin.Password, admin.Salt);
                up.Add("Password", admin.Password);
                up.Add("Salt", admin.Salt);
            }
            return base.Update<Admin>(up, new NameValue<string, object> { { "ID", admin.ID } });
        }
    }
}
